package com.xl.xkit;

import android.util.Log;

import com.xl.xkit.health.AppHealthInfo;
import com.xl.xkit.manager.BlockMonitorManager;
import com.xl.xkit.manager.DataCode;
import com.xl.xkit.manager.PerformanceDataManager;

import java.util.ArrayList;
import java.util.List;

public class AppHealthInfoUtil {
    private AppHealthInfo mAppHealthInfo = new AppHealthInfo();
    private final DataCode dataCode = new DataCode();

    private AppHealthInfoUtil() {
    }

    /**
     * 静态内部类单例
     */
    private static class Holder {
        private static final AppHealthInfoUtil INSTANCE = new AppHealthInfoUtil();
    }

    public static AppHealthInfoUtil getInstance() {
        return AppHealthInfoUtil.Holder.INSTANCE;
    }


    /**
     * 添加cpu信息
     *
     * @param cpuBean CPU数据
     */
    public void addCPUInfo(float cpuBean) {
        dataCode.addCpuData(cpuBean);
    }


    /**
     * 添加memory信息
     *
     * @param memoryBean 内存数据
     */
    public void addMemoryInfo(float memoryBean) {
        dataCode.addMemoryData(memoryBean);
    }

    /**
     * 添加fps信息
     *
     * @param fpsBean fps数据
     */
    public void addFPSInfo(float fpsBean) {
        dataCode.addFpsData(fpsBean);
    }


    /**
     * 添加卡顿信息
     *
     * @param blockBean 卡顿数据
     */
    public void addBlockInfo(AppHealthInfo.DataBean.BlockBean blockBean) {
        List<AppHealthInfo.DataBean.BlockBean> blocks = getData().getBlock();
        if (blocks == null) {
            blocks = new ArrayList<>();
            getData().setBlock(blocks);
        }
        blocks.add(blockBean);
    }


    /**
     * 获取data对象
     *
     * @return 健康信息数据
     */
    private AppHealthInfo.DataBean getData() {
        if (mAppHealthInfo.getData() == null) {
            AppHealthInfo.DataBean dataBean = new AppHealthInfo.DataBean();
            dataBean.setCpu(new ArrayList<>());
            dataBean.setMemory(new ArrayList<>());
            dataBean.setFps(new ArrayList<>());
            dataBean.setNetwork(new ArrayList<>());
            dataBean.setBlock(new ArrayList<>());
            dataBean.setUiLevel(new ArrayList<>());
            dataBean.setLeak(new ArrayList<>());
            dataBean.setPageLoad(new ArrayList<>());
            dataBean.setBigFile(new ArrayList<>());
            dataBean.setSubThreadUI(new ArrayList<>());
            mAppHealthInfo.setData(dataBean);
        }
        return mAppHealthInfo.getData();
    }


    /**
     * 开启健康体检监控
     */
    public void start() {
        PerformanceDataManager.getInstance().init();
        //帧率
        PerformanceDataManager.getInstance().startMonitorFrameInfo();
        //cpu
        PerformanceDataManager.getInstance().startMonitorCPUInfo();
        //内存
        PerformanceDataManager.getInstance().startMonitorMemoryInfo();
        //卡顿
        BlockMonitorManager.getInstance().start();

        BlockMonitorManager.getInstance().setOnBlockInfoUpdateListener(blockInfo -> {

            Log.e("TAG", "s:收到卡顿消息" + blockInfo.toString());

        });
    }

    /**
     * 结束健康体检监控
     */
    public void stop() {
        //帧率
        PerformanceDataManager.getInstance().stopMonitorFrameInfo();
        //cpu
        PerformanceDataManager.getInstance().stopMonitorCPUInfo();
        //内存
        PerformanceDataManager.getInstance().stopMonitorMemoryInfo();
        //卡顿
        BlockMonitorManager.getInstance().stop();
    }


    /**
     * 内存释放
     */
    public void release() {
        if (mAppHealthInfo != null) {
            mAppHealthInfo = null;
        }
    }

}
